var bbtTitleSubtext = getbbtTitleSubtext();
var bbtLegendIcon = 'image://img/overview/bbt.png';

var mensesTitleText = '';
var mensesTitleSubtext = getMensesTitleSubtext();
var mensesLegendIcon = 'image://img/overview/menses.png';

var mucusTitleText = '';
var mucusTitleSubtext = getMucusTitleSubtext();
var mucusLegendIcon = 'image://img/overview/mucus.png';

function getbbtTitleSubtext(){
	var lang = localStorage.getItem("language");

	if(lang == "chinese") return '今日体温';
	else if(lang == "english") return 'Today\'s BBT';
}

function getMensesTitleSubtext(){
	var lang = localStorage.getItem("language");

	if(lang == "chinese") return '月经分析';
	else if(lang == "english") return 'Menstruation';
}

function getMucusTitleSubtext(){
	var lang = localStorage.getItem("language");

	if(lang == "chinese") return '宫颈黏液分析';
	else if(lang == "english") return 'Cervical Mucus';
}

var titlePosition = 'right';
var legendPosition = 'left';

var bbtLineChart;
var mensesBarChart;
var mucusBarChart;

var titleColor = '#666461';
var titleFontSize = 30;
var titleFontWeight = 100;
var legendItemWidth = 20;
var legendItemHeight = 20;

var gridX = 10;
var gridY = 80;
var gridX2 = 10;
var gridY2 = 30;

// 基础体温
//var yAxisMinTemperature = 35.5;
//var yAxisMaxTemperature = 38.5;

// 宫颈黏液
var yAxis_min=0;
var yAxis_max=4;
var yAxis_splitNumber=4;


function setEChartConfig(){
      require.config({
					  paths: {
						echarts: './js'
					  }
				    });
}


/******************************************
  	 画图
   *******************************************/
function drawBBTChart(){
	require([
			'echarts',
			'echarts/chart/line',
	 		],
	 		
	  function (ec) {
		bbtLineChart = ec.init(document.getElementById('bbtLineChart'));		
		setBBTLineChartOption();			
	  });
}

function drawMensesChart(){
	require([
			'echarts',
			'echarts/chart/bar',
	 		],
	 		
	  function (ec) {
		mensesBarChart = ec.init(document.getElementById('mensesBarChart'));
		setMensesBarChartOption();	
	  });
}

function drawMucusChart(){
	require([
			'echarts',
			'echarts/chart/bar',
	 		],
	 		
	  function (ec) {
		mucusBarChart = ec.init(document.getElementById('mucusBarChart'));
		setMucusBarChartOption();	
	  });
}
    
  /****************************************** 
  	 基础体温
   *******************************************/
   
function setBBTLineChartOption(){
	bbtLineChart.setOption({
		  title : {//标题
					text: getDisplayTempValue(getCurrentBBT()) + getTempUnit(),//主标题
					subtext: bbtTitleSubtext,//副标题
					x: titlePosition,//标题位置
					textStyle: {
						color: titleColor,
						fontSize: titleFontSize,
						fontWeight:titleFontWeight
					},
					subtextStyle: {
						fontFamily: "SimHei"
					}
		  },
		  legend: {
		  			show:true,
					itemWidth: legendItemWidth,
					itemHeight: legendItemHeight,
					x:legendPosition,
					selectedMode:false,
					data:[
							  {
								  icon : bbtLegendIcon,
								  name : ' '
							  }
					]
		  },
		  grid:{
				  x:gridX,
				  x2:gridX2,
				  y:0,
				  y2:gridY2,
				  borderWidth: 0
		  },
		  xAxis : [
					{
						show:false,
					  	type : 'category',
					  	data : getCustomerValidDateArray(),
					 }
		  ],
		  yAxis : [
					{
						  show:false,
						  type : 'value',
						  min: getDisplayTempValue(35.5),
                          max: getDisplayTempValue(38.5)
					 }
		  ],		  
		  color:['#4baedf'], //曲线的颜色设置
		  series : [
						{
						  name:'体温',//图表名称
						  type:'line',//图表类型
						  data: getCustomerValidTemperatureArray(),
						  smooth:false,//平滑的曲线
						  symbol: 'none',
						  itemStyle: {
						  				normal: {
						  							lineStyle: {width: 2},
						  							areaStyle: {type: 'default'}//填充显示曲线下区域
						  						}
						  			 },
						},
		  ]
		});
}


   /****************************************** 
  	 月经
   *******************************************/
function setMensesBarChartOption(){
	mensesBarChart.setOption({
			title : {
						text: mensesTitleText,
						subtext: mensesTitleSubtext,//副标题
						x: titlePosition,//标题位置
						textStyle:{
									color: titleColor,
									fontSize: titleFontSize,
									fontWeight:titleFontWeight
						},
						subtextStyle: {
							fontFamily: "SimHei"
						}
			},
			legend: {
						show: true,
						itemWidth: legendItemWidth,
						itemHeight: legendItemHeight,
						x:legendPosition,
						selectedMode:false,
						data:[
							  {
								  icon : mensesLegendIcon,
								  name : ' '
							  }
						  ]
			},			
			grid:{
					x: gridX,
		  			y: gridY,
		  			x2: gridX2,
		  			y2: 0,
				  	borderWidth: 0
			},
			xAxis : [
						{
							show:false,
							type : 'value',
							boundaryGap : [0, 0.01]
						}
			],
			yAxis : [
						{
							show:true,
							axisLine: {show: false},
                            axisTick: {show: false},
                            splitLine: {show: false},
							type : 'category',
							data : getMensesChartLabelArray(),
							axisLabel: {
											margin: -25,
											interval: 0,
											formatter: function(value) {
															return value;
											},
											textStyle: {
												color: "black",
												fontSize: 10,
												fontWeight: "bold"
											}
										}
						}
			],
			
			series : [
						{
							name:'周期',							
							itemStyle:{
										normal: {
													color: 'rgba(240,240,240,0.5)',
													barBorderRadius:0,
													label: {
				    											show: false,
				    											postion: "insideBottom",
				    											textStyle: {color: "black"}
				    										}
												}
							},
							type:'bar',
							barGap: "-100%",
							//stack: 'menses',
							data: getMensesPeriodArray(),
							markLine : {
											symbol: "none",
							                data : [
							                    // 纵轴，默认


							                    	[
							                    	    {name:"周期起点",   xAxis: getAverageMensesPeriod(), yAxis: 0},
							                    	    {name:"周期终点",  xAxis: getAverageMensesPeriod(), yAxis: 100}
							                    	]
							                ],
							                
							                itemStyle:{
					                    					normal:{
					                    								color:'#A3A3A3',
					                    								label: {
					                    											show: false
					                    										},
					                    								lineStyle: {
					                    											 width: 1,
					                    											 type: "dashed"
					                    										   }
					                    							}
				                    				   }
							            }	
						},
						{
							name:'经期',
							itemStyle:{
										normal: {
													color:"rgba(253,105,105,0.5)",
													barBorderRadius:0,
													label: {
                    											show: false,
                    											postion: "insideBottom",
                    											textStyle: {color: "black"}
                    										}				  	
												},
							},
							type:'bar',						
							//stack: 'menses',
							data: getMensesDurationArray(),
							markLine : {
											symbol: "none",
							                data : [
							                    // 纵轴，默认
							                    	[
                                                        {name:"经期起点",   xAxis: getAverageMensesDuration(), yAxis: 0},
                                                        {name:"经期终点",  xAxis: getAverageMensesDuration(), yAxis: 100}
                                                    ]
							                ],
							                
							                itemStyle:{
				                    					normal:{
				                    								color:'#A3A3A3',
				                    								label: {
				                    											show: false
				                    										},
				                    								lineStyle: {
				                    											 width: 1,
				                    											 type: "dashed"
				                    										   }
				                    							}
							                    		}
							            }										
						}
						
						
			]
		});
}


   /****************************************** 
  	 宫颈粘液
   *******************************************/
   
function setMucusBarChartOption(){
	mucusBarChart.setOption({
			title : {
				text: mucusTitleText,
				subtext: mucusTitleSubtext,//副标题
				x: titlePosition,//标题位置
				textStyle:{
							color: titleColor,
							fontSize: titleFontSize,
							fontWeight: titleFontWeight
				},
				subtextStyle: {
					fontFamily: "SimHei"
				}
			},
			legend: {
						show: true,
						itemWidth: legendItemWidth,
                        itemHeight: legendItemHeight,
						x:legendPosition,
						selectedMode:false,
						data:[
							  	{
								  icon : mucusLegendIcon,
								  name : ' '
								 }
						  ]
			},
			grid:{
					x: gridX,
		  			y: gridY,
		  			x2: gridX2,
		  			y2: 1,
		  			borderWidth: 1
			},
			xAxis : [
						{
							type : 'category',
							data: getMucusDateArray(),
							splitNumber: getMucusDateArrayLength(),
							axisLabel:{
										show: false,
										interval: 0,
										formatter: function(value) {
																	 return getDayStr(value);
												   }
									  },
							axisTick:{
								show:false
							},
							axisLine:{
								show:false
							}
						}
			],
			yAxis : [
						{
							type : 'value',
							min:yAxis_min,
							max:yAxis_max,
							splitNumber:yAxis_splitNumber,
							axisLine:{
								show:false
							},							
							axisLabel:{
										show: false
							},
						}
			],
			color:['rgba(218,183,225,0.5)'],
			series : [
						{
							type:'bar',
							barGap:'0%',
							barCategoryGap:'0%',
							itemStyle:{
								normal: {

								},

							},
							data: getMucusDataArray(),
						}
			]
		});
}



/******************************************
  	 获取画图数据
   *******************************************/

function getCustomerValidDateArray(){
     var customerValidDateArrayStr = localStorage.getItem("customerValidDateArrayKey");
     var customerValidDateArray = customerValidDateArrayStr.split(",");

     return customerValidDateArray;
  }

  function getCustomerValidTemperatureArray(){
     var customerValidTemperatureArrayStr = localStorage.getItem("customerValidTemperatureArrayKey");
     var customerValidTemperatureArray = customerValidTemperatureArrayStr.split(",");

     var displayValidTempArray = new Array();

     for(var i = 0; i < customerValidTemperatureArray.length; i++){
     	displayValidTempArray[i] = getDisplayTempValue(customerValidTemperatureArray[i]);
     }

     return displayValidTempArray;
  }

  function getMensesChartLabelArray(){
     var mensesChartLabelArrayStr = localStorage.getItem("mensesChartLabelArrayKey");
     var mensesChartLabelArray = mensesChartLabelArrayStr.split(",");

     return mensesChartLabelArray;
  }

  function getMensesPeriodArray(){
     var mensesPeriodArrayStr = localStorage.getItem("mensesPeriodArrayKey");
     var mensesPeriodArray = mensesPeriodArrayStr.split(",");

     return mensesPeriodArray;
  }

  function getMensesDurationArray(){
     var mensesDurationArrayStr = localStorage.getItem("mensesDurationArrayKey");
     var mensesDurationArray = mensesDurationArrayStr.split(",");

     return mensesDurationArray;
  }

  function getMucusDateArray(){
     var mucusDateArrayStr = localStorage.getItem("mucusDateArrayKey");
     var mucusDateArray = mucusDateArrayStr.split(",");

     return mucusDateArray;
  }

  function getMucusDataArray(){
     var mucusDataArrayStr = localStorage.getItem("mucusDataArrayKey");
     var mucusDataArray = mucusDataArrayStr.split(",");

     return mucusDataArray;
  }

  function getMucusDateArrayLength(){
    var array = getMucusDateArray();

    return array.length;
  }

  function getAverageMensesPeriod(){
    var mensesPeriodArrayStr = localStorage.getItem("mensesPeriodArrayKey");
    var mensesPeriodArray = mensesPeriodArrayStr.split(",");

    var sum = 0;
    for(var i = 0; i < mensesPeriodArray.length; i++ ){
        sum = Number(sum) + Number(mensesPeriodArray[i]);
    }

    return parseFloat(sum / i).toFixed(2);
  }

  function getAverageMensesDuration(){
      var mensesDurationArrayStr = localStorage.getItem("mensesDurationArrayKey");
      var mensesDurationArray = mensesDurationArrayStr.split(",");

      var sum = 0;
      for(var i = 0; i < mensesDurationArray.length; i++ ){
          sum = Number(sum) + Number(mensesDurationArray[i]);
      }

      return parseFloat(sum / i).toFixed(2);
    }

